home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / canmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  3.1 KB  |  87 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "canmain.h"
  10. #include "math.h"
  11. #include <stdlib.h>
  12. //---------------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14. TForm1 *Form1;
  15. //---------------------------------------------------------------------------
  16. __fastcall TForm1::TForm1(TComponent* Owner)
  17.   : TForm(Owner)
  18. {
  19. }
  20. //---------------------------------------------------------------------------
  21. void __fastcall TForm1::FormCreate(TObject *Sender)
  22. {
  23.     Canvas->Pen->Color = clTeal;
  24.     Rotation = 0;
  25.     PointCount = MaxPoints;
  26.     RotatePoints();
  27. }
  28. //---------------------------------------------------------------------
  29. void __fastcall TForm1::FormPaint(TObject *Sender)
  30. {
  31.     int centerX = ClientWidth / 2;
  32.     int centerY = ClientHeight / 2;
  33.     int radius = min(centerY, centerX);
  34.  
  35.     Canvas->Ellipse(0, 0, radius*2, radius*2);
  36.     int i,j;
  37.     for (i = 0; i < PointCount; i++) {
  38.         for (j = i + 1; j < PointCount; j++) {
  39.             Canvas->MoveTo(radius + floor(Points[i].X * radius),
  40.                 radius + floor(Points[i].Y * radius));
  41.             Canvas->LineTo(radius + floor(Points[j].X * radius),
  42.                 radius + floor(Points[j].Y * radius));
  43.         }
  44.     }
  45.  
  46.     // A challenge:  Turn the rotating figure into a ball that bounces off the
  47.     // walls of the window.  Don't forget the english (spin) on the ball should
  48.     // pick up when bouncing off the wall...
  49. }
  50. //---------------------------------------------------------------------
  51. void __fastcall TForm1::FormResize(TObject *Sender)
  52. {
  53.     Invalidate();
  54. }
  55. //---------------------------------------------------------------------
  56. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  57. {
  58.     RotatePoints();
  59.     Invalidate();
  60. }
  61. //---------------------------------------------------------------------
  62. void __fastcall TForm1::RotatePoints()
  63. {
  64.     // NOTE: all figures are in radians
  65.     const float M_2PI = 2 * M_PI;           // 2 pi radians in a circle
  66.     float StepAngle = M_2PI / PointCount;   // angular distance between points
  67.  
  68.     Rotation += M_PI / 32;   // Increment the angle of rotation of figure
  69.     if (Rotation > StepAngle)
  70.         Rotation -= StepAngle;   // Keep rotation less than distance between points
  71.  
  72.     // The loop below has i walking through the Points array, while j walks
  73.     // simultaneously through the angles to each point on the circle.
  74.     // Incrementing j by StepAngle moves j to the next point on the circle with
  75.     // no complicated arithmetic (everything has been set up in advance of the
  76.     // loop).  Initializing j with Rotation causes the entire figure to shift
  77.     // clockwise a small amount.
  78.     //
  79.     int i;
  80.     float j;
  81.     for (i = 0, j = Rotation; i < PointCount; i++, j += StepAngle) {
  82.         Points[i].X = cos(j);   // These values will be multiplied by the
  83.         Points[i].Y = sin(j);   // current radius at display time.
  84.     }
  85. }
  86. //---------------------------------------------------------------------
  87.